package com.hyj.boxueguxiangmu.activity;

import android.os.Bundle;
import android.support.v7.app.AppCompatActivity;
import android.util.Log;
import android.webkit.WebView;
import android.widget.TextView;
import android.widget.Toast;

import com.google.gson.Gson;
import com.hyj.boxueguxiangmu.R;
import com.hyj.boxueguxiangmu.model.NewsTitleModel;

import org.xutils.common.Callback;
import org.xutils.http.RequestParams;
import org.xutils.x;

public class DetailsActivity extends AppCompatActivity {
    WebView textView;
    String contentid;
    TextView titleTv;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_details);
        textView = (WebView) findViewById(R.id.tv);
        titleTv = (TextView) findViewById(R.id.title);
        contentid = getIntent().getStringExtra("contentid");
        loading();
        loadingTitle();
    }


    public void loading(){
        RequestParams params = new RequestParams("http://web.csmzxy.com/netCourse/readData");
        params.addQueryStringParameter("cmd", "9");
        params.addQueryStringParameter("v1", contentid);
        x.http().get(params, new Callback.CommonCallback<String>() {
            @Override
            public void onSuccess(String result) {
                String encoding = "UTF-8";
                String mimeType = "text/html";
                textView.loadDataWithBaseURL("file://", result,mimeType, encoding, "about:blank");
                Log.i("lucashu", "result:" + result);
//                textView.setText(Html.fromHtml(result));
            }

            @Override
            public void onError(Throwable ex, boolean isOnCallback) {
                Toast.makeText(x.app(), ex.getMessage(), Toast.LENGTH_LONG).show();
            }

            @Override
            public void onCancelled(CancelledException cex) {
                Toast.makeText(x.app(), "cancelled", Toast.LENGTH_LONG).show();
            }

            @Override
            public void onFinished() {

            }
        });
    }
    public void loadingTitle() {
        RequestParams params = new RequestParams("http://web.csmzxy.com/netCourse/readData");
        params.addQueryStringParameter("cmd", "41");
        params.addQueryStringParameter("v1", contentid);
        x.http().get(params, new Callback.CommonCallback<String>() {
            @Override
            public void onSuccess(String result) {
                String encoding = "UTF-8";
                String mimeType = "text/html";
                Log.i("lucashu", "result:" + result);
//                textView.setText(Html.fromHtml(result));
                Gson gson = new Gson();
                NewsTitleModel newsTitleModel = gson.fromJson(convertString(result),NewsTitleModel.class);
                titleTv.setText(newsTitleModel.contentTitle);
            }

            @Override
            public void onError(Throwable ex, boolean isOnCallback) {
                Toast.makeText(x.app(), ex.getMessage(), Toast.LENGTH_LONG).show();
            }

            @Override
            public void onCancelled(Callback.CancelledException cex) {
                Toast.makeText(x.app(), "cancelled", Toast.LENGTH_LONG).show();
            }

            @Override
            public void onFinished() {

            }
        });
    }

    public String convertString(String result) {
        String result1 = result
                .replace(", responseSign : 00", "")
                .replace("\'", "\"")
                .replace("contentID", "\"contentID\"")
                .replace("contentAuthor", "\"contentAuthor\"")
                .replace("contentTitle", "\"contentTitle\"")
                .replace("submitTime", "\"submitTime\"");
        return result1;

    }
}
